package com.davinci.freemarket.businessLayer.controllers;

import java.sql.Date;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.davinci.freemarket.dataAccessLayer.accessdata.categorias.CategoriasDAO;
import com.davinci.freemarket.dataAccessLayer.accessdata.comment.CommentDAO;
import com.davinci.freemarket.dataAccessLayer.accessdata.publicacion.PublicacionDAO;
import com.davinci.freemarket.dataModelLayer.model.CommentModel;
import com.davinci.freemarket.dataModelLayer.model.DBAttibuteModel;
import com.davinci.freemarket.dataModelLayer.model.PublicationModel;

public class PublicationsController 
{
	/**
	 * DAO que controla el CRUD de publicaciones a Database
	 */
	protected PublicacionDAO publicaciondao;
	
	/**
	 * DAO que controla el CRUD de comentarios a Database
	 */
	protected CommentDAO commentdao;

	//-------------------------------
	// CONSTRUCTOR
	//-------------------------------
	public PublicationsController() {
		super();
		this.publicaciondao = new PublicacionDAO();
	}
	
	public ArrayList<PublicationModel> selectPublicationByCategory(String search){
		PublicacionDAO publicacionDAO = new PublicacionDAO ();
		ArrayList<PublicationModel> publicaciones = publicacionDAO.selectPublicationByCategory(search);
		return publicaciones;
	}
	
	/**
	 * 
	 * @param params
	 */
	public void addPublication(PublicationModel params) {
		int rs = publicaciondao.addPublicacion( params.getTitle(), params.getDetails(),
												params.getUsr_id(), params.getPrice(),
												params.getTimestamp(), params.getStatus(),
												params.getCategory()
												);
		System.out.println("resultset:\n" + rs);
	}
	public int getLastPublication(){
		int rs = publicaciondao.selectLastId();
		return rs;
	}
	/**
	 * 
	 * @param params
	 */
	public void modifyPublication(PublicationModel params){
		int rs = publicaciondao.modifyPublicacion(	params.getPublication_id(), params.getTitle(), params.getDetails(),
													params.getUsr_id(), params.getPrice(),
													params.getTimestamp(), params.getStatus(),
													params.getCategory()
													);
		System.out.println("resultset:\n" + rs);
	}
	
	/**
	 * 
	 * @param params
	 */
	public void deletePublication(PublicationModel params){
		int rs = publicaciondao.deletePublicacion(params.getPublication_id());
		System.out.println("resultset:\n" + rs);
	}
	
	/**
	 * 
	 * @param params
	 * @return
	 */
	public ArrayList<PublicationModel> getPublication(ArrayList<DBAttibuteModel> conditions) 
	{
		ArrayList<PublicationModel> publications = publicaciondao.selectPublicacion(conditions);
		
		if(publications.size() > 0){
			ArrayList<DBAttibuteModel> commentsConditions;
			for (PublicationModel publicationModel : publications) {
				commentsConditions = new ArrayList<DBAttibuteModel>();
				commentsConditions.add(new DBAttibuteModel("ID_PUBLI", publicationModel.getPublicationId()));
				
				commentdao = new CommentDAO();
				ResultSet commsResultset = commentdao.selectComments(commentsConditions, "=");
				fillComments(publicationModel, commsResultset);
			}
		}
		
		return publications;
	}
	
	public ArrayList<PublicationModel> getMasVendidos() 
	{
		ArrayList<PublicationModel> publications = publicaciondao.selectMasVendidos();
		return publications;
	}
	public ArrayList<PublicationModel> getRecienLlegados() 
	{
		ArrayList<PublicationModel> publications = publicaciondao.selectRecienLlegados();
		return publications;
	}
	
	/**
	 * 
	 * @param rs
	 * @param commsResultset
	 */
	protected void fillComments(PublicationModel publication, ResultSet commsResultset) 
	{
		ArrayList<CommentModel> comments = new ArrayList<CommentModel>();
		CommentModel comm;
		
		try {
			while (commsResultset.next()) {
				comm = new CommentModel();
				comm.setCommentID(commsResultset.getInt("ID_COMM"));
				comm.setUserID(commsResultset.getInt("ID_USER"));
				comm.setPublicationID(commsResultset.getInt("ID_PUBLI"));
				comm.setAnswered(commsResultset.getInt("RESPONDIDO"));
				comm.setQuestion(commsResultset.getString("PREGUNTA"));
				comm.setResponse(commsResultset.getString("RESPUESTA"));
				
				comments.add(comm);
			}
			
			publication.setComments(comments);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 
	 * @param search
	 * @return
	 */
	public ArrayList<PublicationModel> searchPublications(String search)
	{
		PublicacionDAO publicacion = new PublicacionDAO();
		ArrayList<PublicationModel> publications = publicacion.search(search);
		return publications;
	}

}
